package com.mls.fivedistrict.controller.backend;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.pojo.User;
import com.mls.fivedistrict.pojo.UserVoteOption;
import com.mls.fivedistrict.pojo.Vote;
import com.mls.fivedistrict.server.UserService;
import com.mls.fivedistrict.server.VoteService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 投票 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-05-21
 */
@RestController
@RequestMapping("/api/manager")
public class VoteController {
    @Autowired
    private VoteService voteService;
    @Autowired
    private UserService userService;

    @RequestMapping("/v1/votes")
    public ServiceResponse getVoteList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<Vote> voteList = voteService.selectListAndVoteOption(pageFilter.getWrapper());
        PageInfo<Vote> pageInfo = new PageInfo<>(voteList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @GetMapping("/v1/vote/{voteId}")
    public ServiceResponse getVote(@PathVariable("voteId") Long voteId) {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        Vote vote = voteService.selectVoteAndVoteOption(voteId);
        return ServiceResponse.createSuccessResponse(vote);
    }

    @PostMapping("/v1/vote")
    public ServiceResponse insert(@RequestBody Vote vote) {
        BeanValidator.check(vote);
        boolean insert = voteService.insertAndVoteOptions(vote);
        if (!insert) {
            return ServiceResponse.createErrorResponse("插入失败");
        }
        return ServiceResponse.createSuccessResponse("插入成功");
    }

    @PutMapping("/v1/vote/{voteId}")
    public ServiceResponse update(@PathVariable("voteId") Long voteId, @RequestBody Vote vote) {
        vote.setId(voteId);
        boolean update = voteService.updateVoteAndVoteOption(vote);
        if (!update) {
            return ServiceResponse.createErrorResponse("修改失败");
        }
        return ServiceResponse.createSuccessResponse("修改成功");
    }

    @DeleteMapping("/v1/vote/{voteId}")
    public ServiceResponse delete(@PathVariable("voteId") Long voteId) {
//        boolean delete = voteService.deleteById(voteId);
        Vote vote = new Vote();
        vote.setId(voteId);
        vote.setIsDelete(true);
        boolean delete = voteService.updateById(vote);
        if (!delete) {
            return ServiceResponse.createErrorResponse("删除失败");
        }
        return ServiceResponse.createSuccessResponse("删除成功");
    }

    @RequestMapping("/v1/vote/users")
    public ServiceResponse userList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<UserVoteOption> userVoteOptionList = userService.getVoteUserList(pageFilter);
        PageInfo pageInfo = new PageInfo(userVoteOptionList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

}
